//    Copyright (C) Clear Office.  All rights reserved.
using System;
using System.Collections.Generic;
using Office.Core.Serialization;

namespace Office.Spreadsheet.PivotTable
{
    internal class PivotField : IOfficeXmlSerializable
    {
        private string _name;
        private Axis? _axis;
        private bool _dataField;
        private string _subtotalCaption;
        private bool _showDropDowns;
        private bool _hiddenLevel;
        private string _uniqueMemberProperty;
        private bool _compact;
        private bool _allDrilled;
        private int? _numFmtId;
        private bool _outline;
        private bool _subtotalTop;
        private bool _dragToRow;
        private bool _dragToCol;
        private bool _multipleItemSelectionAllowed;
        private bool _dragToPage;
        private bool _dragToData;
        private bool _dragOff;
        private bool _showAll;
        private bool _insertBlankRow;
        private bool _serverField;
        private bool _insertPageBreak;
        private bool _autoShow;
        private bool _topAutoShow;
        private bool _hideNewItems;
        private bool _measureFilter;
        private bool _includeNewItemsInFilter;
        private int _itemPageCount;
        private FieldSortType _sortType;
        private bool? _dataSourceSort;
        private bool _nonAutoSortDefault;
        private int? _rankBy;
        private bool _defaultSubtotal;
        private bool _sumSubtotal;
        private bool _countASubtotal;
        private bool _avgSubtotal;
        private bool _maxSubtotal;
        private bool _minSubtotal;
        private bool _productSubtotal;
        private bool _countSubtotal;
        private bool _stdDevSubtotal;
        private bool _stdDevPSubtotal;
        private bool _varSubtotal;
        private bool _varPSubtotal;
        private bool _showPropCell;
        private bool _showPropTip;
        private bool _showPropAsCaption;
        private bool _defaultAttributeDrillState;
        private AutoSortScope _autoSortScope;
        private List<Item> _items = new List<Item>();

        public void ReadXml(ReadContext context)
        {
            using (var c = context.Read("pivotField"))
            {
                _name = c.GetOptionalString("name");
                _axis = c.GetOptionalEnum<Axis>("axis");
                _dataField = c.GetOptionalBoolean("dataField", false);
                _subtotalCaption = c.GetOptionalString("subtotalCaption");
                _showDropDowns = c.GetOptionalBoolean("showDropDowns", true);
                _hiddenLevel = c.GetOptionalBoolean("hiddenLevel", false);
                _uniqueMemberProperty = c.GetOptionalString("uniqueMemberProperty");
                _compact = c.GetOptionalBoolean("compact", true);
                _allDrilled = c.GetOptionalBoolean("allDrilled", false);
                _numFmtId = c.GetOptionalInteger("numFmtId"); //\\ do lookup
                _outline = c.GetOptionalBoolean("outline", true);
                _subtotalTop = c.GetOptionalBoolean("subtotalTop", true);
                _dragToRow = c.GetOptionalBoolean("dragToRow", true);
                _dragToCol = c.GetOptionalBoolean("dragToCol", true);
                _multipleItemSelectionAllowed = c.GetOptionalBoolean("multipleItemSelectionAllowed", false);
                _dragToPage = c.GetOptionalBoolean("dragToPage", true);
                _dragToData = c.GetOptionalBoolean("dragToData", true);
                _dragOff = c.GetOptionalBoolean("dragOff", true);
                _showAll = c.GetOptionalBoolean("showAll", true);
                _insertBlankRow = c.GetOptionalBoolean("insertBlankRow", false);
                _serverField = c.GetOptionalBoolean("serverField", false);
                _insertPageBreak = c.GetOptionalBoolean("insertPageBreak", false);
                _autoShow = c.GetOptionalBoolean("autoShow", false);
                _topAutoShow = c.GetOptionalBoolean("topAutoShow", true);
                _hideNewItems = c.GetOptionalBoolean("hideNewItems", false);
                _measureFilter = c.GetOptionalBoolean("measureFilter", false);
                _includeNewItemsInFilter = c.GetOptionalBoolean("includeNewItemsInFilter", false);
                _itemPageCount = c.GetOptionalInteger("itemPageCount", 10);
                _sortType = c.GetOptionalEnum("sortType", FieldSortType.Manual);
                _dataSourceSort = c.GetOptionalBoolean("dataSourceSort");
                _nonAutoSortDefault = c.GetOptionalBoolean("nonAutoSortDefault", false);
                _rankBy = c.GetOptionalInteger("rankBy");
                _defaultSubtotal = c.GetOptionalBoolean("defaultSubtotal", true);
                _sumSubtotal = c.GetOptionalBoolean("sumSubtotal", false);
                _countASubtotal = c.GetOptionalBoolean("countASubtotal", false);
                _avgSubtotal = c.GetOptionalBoolean("avgSubtotal", false);
                _maxSubtotal = c.GetOptionalBoolean("maxSubtotal", false);
                _minSubtotal = c.GetOptionalBoolean("minSubtotal", false);
                _productSubtotal = c.GetOptionalBoolean("productSubtotal", false);
                _countSubtotal = c.GetOptionalBoolean("countSubtotal", false);
                _stdDevSubtotal = c.GetOptionalBoolean("stdDevSubtotal", false);
                _stdDevPSubtotal = c.GetOptionalBoolean("stdDevPSubtotal", false);
                _varSubtotal = c.GetOptionalBoolean("varSubtotal", false);
                _varPSubtotal = c.GetOptionalBoolean("varPSubtotal", false);
                _showPropCell = c.GetOptionalBoolean("showPropCell", false);
                _showPropTip = c.GetOptionalBoolean("showPropTip", false);
                _showPropAsCaption = c.GetOptionalBoolean("showPropAsCaption", false);
                _defaultAttributeDrillState = c.GetOptionalBoolean("defaultAttributeDrillState", false);

                _items.Clear();
                _items.AddRange(c.ReadOptionalListElement<Item>("items", "item"));
                this._autoSortScope = c.ReadOptionalElement<AutoSortScope>("autoSortScope");
                c.WarnIf("extLst");
            }

        }

        public void WriteXml(WriteContext context)
        {
            using (var c = context.Write("pivotField"))
            {
                c.SetOptionalString("name", _name);
                c.SetOptionalEnum<Axis>("axis", _axis);
                c.SetOptionalBoolean("dataField", _dataField, false);
                c.SetOptionalString("subtotalCaption", _subtotalCaption);
                c.SetOptionalBoolean("showDropDowns", _showDropDowns, true);
                c.SetOptionalBoolean("hiddenLevel", _hiddenLevel, false);
                c.SetOptionalString("uniqueMemberProperty", _uniqueMemberProperty);

                c.SetOptionalBoolean("compact", _compact, true);
                c.SetOptionalBoolean("allDrilled", _allDrilled, false);
                c.SetOptionalInteger("numFmtId", _numFmtId);

                c.SetOptionalBoolean("outline", _outline, true);
                c.SetOptionalBoolean("subtotalTop", _subtotalTop, true);
                c.SetOptionalBoolean("dragToRow", _dragToRow, true);
                c.SetOptionalBoolean("dragToCol", _dragToCol, true);
                c.SetOptionalBoolean("multipleItemSelectionAllowed", _multipleItemSelectionAllowed, false);
                c.SetOptionalBoolean("dragToPage", _dragToPage, true);
                c.SetOptionalBoolean("dragToData", _dragToData, true);
                c.SetOptionalBoolean("dragOff", _dragOff, true);
                c.SetOptionalBoolean("showAll", _showAll, true);
                c.SetOptionalBoolean("insertBlankRow", _insertBlankRow, false);
                c.SetOptionalBoolean("serverField", _serverField, false);
                c.SetOptionalBoolean("insertPageBreak", _insertPageBreak, false);
                c.SetOptionalBoolean("autoShow", _autoShow, false);
                c.SetOptionalBoolean("topAutoShow", _topAutoShow, true);
                c.SetOptionalBoolean("hideNewItems", _hideNewItems, false);
                c.SetOptionalBoolean("measureFilter", _measureFilter, false);
                c.SetOptionalBoolean("includeNewItemsInFilter", _includeNewItemsInFilter, false);
                c.SetOptionalInteger("itemPageCount", _itemPageCount, 10);
                c.SetOptionalEnum("sortType", _sortType, FieldSortType.Manual);

                c.SetOptionalBoolean("dataSourceSort", _dataSourceSort);
                c.SetOptionalBoolean("nonAutoSortDefault", _nonAutoSortDefault, false);
                c.SetOptionalInteger("rankBy", _rankBy);
                c.SetOptionalBoolean("defaultSubtotal", _defaultSubtotal, true);
                c.SetOptionalBoolean("sumSubtotal", _sumSubtotal, false);
                c.SetOptionalBoolean("countASubtotal", _countASubtotal, false);
                c.SetOptionalBoolean("avgSubtotal", _avgSubtotal, false);
                c.SetOptionalBoolean("maxSubtotal", _maxSubtotal, false);
                c.SetOptionalBoolean("minSubtotal", _minSubtotal, false);
                c.SetOptionalBoolean("productSubtotal", _productSubtotal, false);
                c.SetOptionalBoolean("countSubtotal", _countSubtotal, false);
                c.SetOptionalBoolean("stdDevSubtotal", _stdDevSubtotal, false);
                c.SetOptionalBoolean("stdDevPSubtotal", _stdDevPSubtotal, false);
                c.SetOptionalBoolean("varSubtotal", _varSubtotal, false);
                c.SetOptionalBoolean("varPSubtotal", _varPSubtotal, false);
                c.SetOptionalBoolean("showPropCell", _showPropCell, false);
                c.SetOptionalBoolean("showPropTip", _showPropTip, false);
                c.SetOptionalBoolean("showPropAsCaption", _showPropAsCaption, false);
                c.SetOptionalBoolean("defaultAttributeDrillState", _defaultAttributeDrillState, false);
                if (_items.Count > 0)
                {
                    using (var c2 = c.Write("items"))
                    {
                        c2.SetInteger("count", _items.Count);
                        c2.WriteElements(_items);
                    }
                }
                c.WriteOptionalElement(_autoSortScope);
                c.WarnIf("extLst");

            }
        }
    }
}